1 package activity;
2
3 import java.lang.*;
4 import javax.swing.*;
5 import java.awt.*;
6 import java.awt.
event.*;
7 import attr.*;

8
9 public
class ManageProduct extends JFrame implements ActionListener {
10     
private JPanel panel;
11     ViewProductActivity prev;
12     
private Product product;
13     
private JButton buttonBack, buttonEdit, buttonDelete, buttonSell, buttonSubmit;
14     
private JLabel title, header, productIdLabel, productNameLabel, productQtLabel, productPriceLabel, userIdLabel;
15     
private JTextField productIdTF, productNameTF, productQtTF, productPriceTF, userIdTF;
16     
17     
public ManageProduct(String pid, ViewProductActivity prev) {
18         super(
"Manage Product");
19         
20         
this.setSize(500,400);
21         
this.setResizable(false);
22         
this.setLocationRelativeTo(null);
23         
this.prev = prev;
24         
25         product =
new Product(pid);
26         product.fetch();
27         
28         panel =
new JPanel();
29         panel.setLayout(
null);
30         panel.setBackground(Theme.BACKGROUND_PANEL);
31         
32         productIdLabel =
new JLabel("Product ID: "+product.getProductId());
33         productIdLabel.setBounds(
60, 20, 140, 30);
34         productIdLabel.setFont(Theme.FONT_INPUT);
35         panel.
add(productIdLabel);
36         
37         productNameLabel =
new JLabel("Name: ");
38         productNameLabel.setBounds(
60, 60, 140, 30);
39         productNameLabel.setFont(Theme.FONT_INPUT);
40         panel.
add(productNameLabel);
41         
42         productPriceLabel =
new JLabel("Price: ");
43         productPriceLabel.setBounds(
60, 100, 140, 30);
44         productPriceLabel.setVisible(
false);
45         productPriceLabel.setFont(Theme.FONT_INPUT);
46         panel.
add(productPriceLabel);
47         
48         userIdLabel =
new JLabel("CustomerID: ");
49         userIdLabel.setBounds(
60, 100, 140, 30);
50         userIdLabel.setFont(Theme.FONT_INPUT);
51         panel.
add(userIdLabel);
52         
53         productQtLabel =
new JLabel("Quantity: ");
54         productQtLabel.setBounds(
60, 140, 140, 30);
55         productQtLabel.setFont(Theme.FONT_INPUT);
56         panel.
add(productQtLabel);
57         
58         productNameTF =
new JTextField(product.getProductName());
59         productNameTF.setBounds(
180, 60, 220, 30);
60         productNameTF.setEnabled(
false);
61         productNameTF.setFont(Theme.FONT_INPUT);
62         panel.
add(productNameTF);
63         
64         userIdTF =
new JTextField("");
65         userIdTF.setBounds(
180, 100, 220, 30);
66         userIdTF.setFont(Theme.FONT_INPUT);
67         panel.
add(userIdTF);
68         
69         productPriceTF =
new JTextField(product.getPrice()+"");
70         productPriceTF.setBounds(
180, 100, 220, 30);
71         productPriceTF.setFont(Theme.FONT_INPUT);
72         productPriceTF.setVisible(
false);
73         panel.
add(productPriceTF);
74         
75         productQtTF =
new JTextField("");
76         productQtTF.setBounds(
180, 140, 220, 30);
77         productQtTF.setFont(Theme.FONT_INPUT);
78         panel.
add(productQtTF);
79         
80         buttonEdit =
new JButton("Edit");
81         buttonEdit.setBounds(
180, 180, Theme.BUTTON_PRIMARY_WIDTH,30);
82         buttonEdit.setFont(Theme.FONT_BUTTON);
83         buttonEdit.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
84         buttonEdit.setForeground(Theme.COLOR_BUTTON_PRIMARY);
85         buttonEdit.addActionListener(
this);
86         panel.
add(buttonEdit);
87         
88         buttonSubmit =
new JButton("Submit");
89         buttonSubmit.setBounds(
180, 180, Theme.BUTTON_PRIMARY_WIDTH,30);
90         buttonSubmit.setFont(Theme.FONT_BUTTON);
91         buttonSubmit.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
92         buttonSubmit.setForeground(Theme.COLOR_BUTTON_PRIMARY);
93         buttonSubmit.setVisible(
false);
94         buttonSubmit.addActionListener(
this);
95         panel.
add(buttonSubmit);
96         
97         buttonDelete =
new JButton("Delete");
98         buttonDelete.setBounds(
300, 180, Theme.BUTTON_PRIMARY_WIDTH,30);
99         buttonDelete.setFont(Theme.FONT_BUTTON);
100         buttonDelete.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
101         buttonDelete.setForeground(Theme.COLOR_BUTTON_PRIMARY);
102         buttonDelete.addActionListener(
this);
103         panel.
add(buttonDelete);
104         
105         buttonSell =
new JButton("Sell");
106         buttonSell.setBounds(
60, 180, Theme.BUTTON_PRIMARY_WIDTH,30);
107         buttonSell.setFont(Theme.FONT_BUTTON);
108         buttonSell.setBackground(Theme.BACKGROUND_BUTTON_PRIMARY);
109         buttonSell.setForeground(Theme.COLOR_BUTTON_PRIMARY);
110         buttonSell.addActionListener(
this);
111         panel.
add(buttonSell);
112         
113         
this.add(panel);
114     }
115     
116     
public void actionPerformed(ActionEvent ae) {
117         
if (ae.getSource().equals(buttonSell)) {
118             
try {
119                 product.sellProduct(userIdTF.getText().trim(),Integer.parseInt(productQtTF.getText()));
120                 
if (!prev.keywordTF.getText().trim().isEmpty())
121                     prev.table.setModel(Product.searchProduct(prev.keywordTF.getText().trim(), prev.byWhatCB.getSelectedItem().toString()));
122                 
else
123                     prev.table.setModel(Product.searchProduct(
"", "By Name"));
124                 
this.setVisible(false);
125             }
126             
catch (NumberFormatException e) {
127                 JOptionPane.showMessageDialog(
this,"Invalid Input!");
128             }
129         }
130         
else if (ae.getSource().equals(buttonEdit)) {
131             buttonEdit.setVisible(
false);
132             buttonSubmit.setVisible(
true);
133             buttonSell.setEnabled(
false);
134             productQtTF.setText(product.getQuantity()+
"");
135             productNameTF.setEnabled(
true);
136             userIdLabel.setVisible(
false);
137             userIdTF.setVisible(
false);
138             productPriceLabel.setVisible(
true);
139             productPriceTF.setVisible(
true);
140         }
141         
else if (ae.getSource().equals(buttonSubmit)) {
142             
try {
143                 product.updateProduct(productNameTF.getText(),Double.parseDouble(productPriceTF.getText()),Integer.parseInt(productQtTF.getText()));
144                 
if (!prev.keywordTF.getText().trim().isEmpty())
145                     prev.table.setModel(Product.searchProduct(prev.keywordTF.getText().trim(), prev.byWhatCB.getSelectedItem().toString()));
146                 
else
147                     prev.table.setModel(Product.searchProduct(
"", "By Name"));
148                 
this.setVisible(false);
149             }
150             
catch (NumberFormatException e) {
151                 JOptionPane.showMessageDialog(
this,"Invalid Input!");
152             }
153         }
154         
else if (ae.getSource().equals(buttonDelete)) {
155             product.deleteProduct();
156             
if (!prev.keywordTF.getText().trim().isEmpty())
157                 prev.table.setModel(Product.searchProduct(prev.keywordTF.getText().trim(), prev.byWhatCB.getSelectedItem().toString()));
158             
else
159                 prev.table.setModel(Product.searchProduct(
"", "By Name"));
160             
this.setVisible(false);
161         }
162         
else {}
163     }
164 }


Gõ tìm kiếm nhanh...